package com.itheima.stock.service.imp;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.base.Strings;
import com.itheima.stock.mapper.SysLogMapper;
import com.itheima.stock.pojo.SysLog;
import com.itheima.stock.service.LogService;
import com.itheima.stock.vo.code.ResponseCode;
import com.itheima.stock.vo.req.LogReqVo;
import com.itheima.stock.vo.resp.PageResult;
import com.itheima.stock.vo.result.R;
import org.apache.catalina.LifecycleState;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * @author: siwukun
 * @date: 2022/06/11
 * @description:
 */
@Service
public class LogServiceImpl implements LogService {

    @Autowired
    private SysLogMapper logMapper;
    /**
     * 获取所有的日志信息
     *
     * @param reqVo 请求参数
     * @return
     */
    @Override
    public R<PageResult<SysLog>> getAllLogs(LogReqVo reqVo) {
        if (reqVo == null){
            return R.error(ResponseCode.DATA_ERROR.getMessage());
        }
        //设置分页查询参数
        PageHelper.startPage(reqVo.getPageNum(),reqVo.getPageSize());
        //获取分页后的数据
        List<SysLog> pageList  = logMapper.getAllPageLogs(reqVo);
        //获取分页后的参数
        PageInfo<SysLog> pageInfo = new PageInfo<>(pageList);
        //封装数据响应给前端
        return R.ok(new PageResult(pageInfo));
    }

    /**
     * 批量删除日志信息
     * @param lid 日志id
     * @return
     */
    @Override
    public R<String> deleteAllLogs(List<Long> lid) {
        if (lid ==null){
            return R.error(ResponseCode.DATA_ERROR.getMessage());
        }
        //根据id删除log
        boolean flag  = logMapper.deleteById(lid);
        if (!flag){
            return R.error(ResponseCode.SYSTEM_ERROR.getMessage());
        }
        return R.ok(ResponseCode.SUCCESS.getMessage());
    }
}
